﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace Xcap
{
    public class XcapProcessor : XcapCore
    {
        public event DataRecievedEventHandler DataRecievedEvent;

        private XcapRequest request;
        protected string targetUser;

        //ak ide o pridanie entry tak sem sa ulozi nazov listu
        //do ktore sa ide ukladat, pripadne mazat
        protected string resourceListsEntryList;
        //sem ulozi URI usera, ktory sa pridava, lebo podla toho
        //vklada noveho uzivatela na koniec
        protected string resourceListsEntryUri;

        protected string presRulesRuleId;
        protected string presRulesIdentityOne;

        protected XcapProcessor(Config _config, Account _account) : base( _config, _account)
        {
            targetUser = _account.SipUri;
            //Console.WriteLine(targetUser);
            //objekt na requesty
            request = new XcapRequest(config, account);
        }

        /// <summary>
        /// Method that generates specified http request using XcapRequest class
        /// </summary>
        /// <param name="auth">Set true if authorization is required</param>
        /// <param name="action">xcapAction action (GET, PUT, DELETE)</param>
        /// <param name="document">xcapDocument which is handeled</param>
        protected void HandleRequest(bool auth, xcapAction action, xcapDocument document)
        {
            switch (document)
            { 
                case xcapDocument.servCaps:
                    request.HttpUri = "xcap-caps/global/index";
                    break;

                case xcapDocument.resourceLists:
                    request.HttpUri = "resource-lists/users/" + targetUser + "/index";
                    break;

                case xcapDocument.resourceListsEntry:
                    //ak ide o DELETE alebo o GET
                    if (action != xcapAction.PUT)
                    {
                        request.HttpUri = "resource-lists/users/" + targetUser + "/index/~~/resource-lists/list%5b@name=%22" + resourceListsEntryList + "%22%5d/entry%5b@uri=%22" + resourceListsEntryUri + "%22%5d";
                    }
                    break;

                case xcapDocument.presRules:
                    request.HttpUri = "pres-rules/users/" + targetUser + "/index";
                    break;

                case xcapDocument.presRulesIdentity:
                    if (action != xcapAction.PUT)
                    {
                        request.HttpUri = "pres-rules/users/" + targetUser + "/index/~~/a:ruleset/a:rule%5b@id=%22" + presRulesRuleId + "%22%5d/a:conditions/a:identity/a:one%5b@id=%22" + presRulesIdentityOne + "%22%5d?xmlns(a=urn:ietf:params:xml:ns:common-policy)";                        
                    }
                    break;
                case xcapDocument.pidf:
                    //rcf 4827(xcap pidf) hovori ze subor musi byt /users/userX/index
                    request.HttpUri = "pidf-manipulation/users/" + targetUser + "/index";
                    break;
            }

            request.RequestType = action;
            request.Auth = auth;

            string data = Send(request.BasicRequest(document));
            OnDataRecievedEvent(new DataRecievedEventArgs(data));
        }
        /// <summary>
        /// Method that generates specified http request using XcapRequest class
        /// </summary>
        /// <param name="auth">Set true if authorization is required</param>
        /// <param name="action">xcapAction action (GET, PUT, DELETE)</param>
        /// <param name="document">xcapDocument which is handeled</param>
        /// <param name="content">content which is being upload on server</param>
        protected void HandleRequest(bool auth, xcapAction action, xcapDocument document, string content)
        {
            switch (document)
            {
                case xcapDocument.pidf:
                    //rcf 4827(xcap pidf) hovori ze subor musi byt /users/userX/index
                    request.HttpUri = "pidf-manipulation/users/" + account.SipUri + "/index";

                    if (action == xcapAction.PUT)
                    {
                        request.XmlContent = content;
                    }
                    break;

                case xcapDocument.resourceLists:
                    
                    request.HttpUri = "resource-lists/users/" + targetUser + "/index";
                    
                    if (action == xcapAction.PUT)
                    {
                        request.XmlContent = content;
                    }
                    break;

                case xcapDocument.resourceListsEntry:

                    //ak ide o pridanie iba jednej entry do list elementu
                    //To create this element within the document, the node selector is    
                    //constructed such that it is a no-match against the current document,    
                    //but if the element in the body of the request was added to the    
                    //document as desired by the client
                    request.HttpUri = "resource-lists/users/" + targetUser + "/index/~~/resource-lists/list%5b@name=%22" + resourceListsEntryList + "%22%5d/entry%5b@uri=%22" + resourceListsEntryUri + "%22%5d";

                    if (action == xcapAction.PUT)
                    {
                        request.XmlContent = content;
                    }
                    break;

                case xcapDocument.presRules:

                    request.HttpUri = "pres-rules/users/" + targetUser + "/index";

                    if (action == xcapAction.PUT)
                    {
                        request.XmlContent = content;
                    }
                    break;

                case xcapDocument.presRulesIdentity:

                    request.HttpUri = "pres-rules/users/" + targetUser + "/index/~~/a:ruleset/a:rule%5b@id=%22" + presRulesRuleId + "%22%5d/a:conditions/a:identity/a:one%5b@id=%22" + presRulesIdentityOne + "%22%5d?xmlns(a=urn:ietf:params:xml:ns:common-policy)";

                    if (action == xcapAction.PUT)
                    {
                        request.XmlContent = content;
                    }
                    break;

                default:
                    throw (new NotImplementedException());
            }

            request.RequestType = action;
            request.Auth = auth;

            string data = Send(request.BasicRequest(document));
            OnDataRecievedEvent(new DataRecievedEventArgs(data));
        }

        protected virtual void OnDataRecievedEvent(DataRecievedEventArgs e)
        {
            DataRecievedEvent(this, e);
        }

    }
}
